<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js"></script>
<title></title>
<script type="text/javascript">
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var resultWidth = Ext.get("process-grid").getWidth();
		var userId = '${__SATURN_AUTORITY_USER__.id}';
		var states = [['2','运行'], ['7','完成']];
        //用于下拉列表的store
        var simpleStore = new Ext.data.SimpleStore({
            fields: ['state','stateName']
        });
        simpleStore.loadData(states);

		var simple = new Ext.FormPanel({
			renderTo:'form-ct',
	        frame:true,
	        title: '查询条件',
	        labelWidth:resultWidth*0.1,
	        bodyStyle:'width:100%',
	       items: [{
	            layout:'column',
	            items:[{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '流程定义ID',
		                    name: 'process_define_id',
		                    id: 'process_define_id',
		                    vtype:'isNumber',
		                    maxLength:10,
		                    msgTarget:'qtip'	                                
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '流程实例名称',
		                    name: 'process_instance_name',
		                    id: 'process_instance_name',
		                    vtype:'isNumberAlphaUnderlineChinese',
		                    maxLength:10,
		                    msgTarget:'qtip'
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    allowDecimals:false,
		                    fieldLabel: '流程实例ID',
		                    name: 'instance_id',
		                    id: 'instance_id',
		                    vtype:'isNumber',
		                    maxLength:10,
		                    msgTarget:'qtip'
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '流程实例描述',
		                    name: 'process_instance_desc',
		                    id: 'process_instance_desc',
		                    maxLength:10,
		                    vtype:'isNumberAlphaUnderlineChinese',
		                    msgTarget:'qtip'
		                }]
	            },{
	            	columnWidth:.23,
	                layout: 'form',
	                items: [{
		                    xtype:'datefield',
		                    fieldLabel: '流程开始时间',
		                    name: 'start_time_from',
		                    id: 'start_time_from',
		                     readOnly:true
		                }]
	            },{
	                layout: 'column',
	               	columnWidth:.27,
	                items: [{
		                    xtype:'label',
		                    text: '--'
		                },{
		                    xtype:'datefield',
		                    name: 'start_time_to',
		                    id: 'start_time_to',
		                     readOnly:true
		                }]
	            },{
	            	columnWidth:.23,
	                layout: 'form',
	                items: [{
		                    xtype:'datefield',
		                    fieldLabel: '流程结束时间',
		                    name: 'end_time_from',
		                    id: 'end_time_from',
		                     readOnly:true
		                }]
	            },{
	                layout: 'column',
	                columnWidth:.27,
	                items: [{
		                    xtype:'label',
		                    text: '--'
		                },{
		                    xtype:'datefield',
		                    name: 'end_time_to',
		                    id: 'end_time_to',
		                     readOnly:true
		                }]
	            },
	            {
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'combo',
		                    fieldLabel: '当前状态',
		                    name:'current_state_show',
		                    hiddenName:'process_instance_state',
		                    id: 'current_state',
		                    store: simpleStore,
		                    displayField: 'stateName',
		                    valueField: 'state',
		                    mode: 'local',
						    triggerAction: 'all',
						    emptyText:'请选择流程当前状态...',
						    width:130,
						    editable: false,
 							typeAhead: true
		                }]
	            }] 
        }],

	        buttons: [{
	            text: '确定',
	            handler:function(){
	             if(simple.form.isValid()){
	                var start_time_from = Ext.get("start_time_from").dom.value;
		            var start_time_to = Ext.get("start_time_to").dom.value;
		            var end_time_from = Ext.get("end_time_from").dom.value;
		            var end_time_to = Ext.get("end_time_to").dom.value;
		            if(validate(start_time_from,start_time_to,end_time_from,end_time_to)){
			            if(start_time_from!=''){
			            	start_time_from = start_time_from+" 00:00:00";
			            }
			             if(start_time_to!=''){
			             	start_time_to = start_time_to+" 23:59:59";
			            }
		    	        if(end_time_from!=''){
		            		end_time_from = end_time_from+" 00:00:00";
		            	}
			            if(end_time_to!=''){
			            	end_time_to = end_time_to+" 23:59:59";
				        }
						store.load({params: {start:0,
									limit:20,
									creator:userId,
									process_define_id:Ext.get("process_define_id").dom.value,
									name:Ext.get("process_instance_name").dom.value,
									process_instance_id:Ext.get("instance_id").dom.value,
									description:Ext.get("process_instance_desc").dom.value,
									start_time_from:start_time_from,
									start_time_to:start_time_to,
									end_time_from:end_time_from,
									end_time_to:end_time_to,
									process_instance_state:Ext.get("process_instance_state").getValue()}});
		            }
		            }
	            }
	        },{
	            text: '重置',
	            handler:function(){
	            	simple.getForm().reset();
	            }
	        }]
	    });
		
		var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myProcessModule/action/QueryStartedProList.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'process_define_id', mapping: 'process_define_id'},
	               {name: 'process_instance_id', mapping: 'process_instance_id'},
	               {name: 'process_instance_name', mapping: 'name'},
		    	   {name: 'process_instance_desc', mapping: 'description'},
		    	   {name: 'process_instance_state', mapping: 'state'},
	               {name: 'proc_inst_start_time', mapping: 'start_time'},
	               {name: 'proc_inst_end_time', mapping: 'end_time'}
	           ]
		});
		
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [
	       	    {header: "操作", width:resultWidth*0.1, dataIndex: 'process_define_id', sortable: false,renderer:showOp},
	       	    {header: "流程定义ID", width: resultWidth*0.1, dataIndex: 'process_define_id', sortable: true},
	            {header: "流程实例ID", width:resultWidth*0.1, dataIndex: 'process_instance_id', sortable: true},
	            {header: "流程实例名称", width: resultWidth*0.15, dataIndex: 'process_instance_name', sortable: true},
	            {header: "流程实例描述", width: resultWidth*0.15, dataIndex: 'process_instance_desc', sortable: true},
	            {header: "当前状态", width: resultWidth*0.1, dataIndex: 'process_instance_state', sortable: true,renderer:showState},
	            {header: "流程开始时间", width:resultWidth*0.15, dataIndex: 'proc_inst_start_time', sortable: true},
	            {header: "流程结束时间", width:resultWidth*0.15, dataIndex: 'proc_inst_end_time', sortable: true}
	        ],
	        renderTo:'process-grid',
	        height:400,
	        frame:true,
	        bodyStyle:'width:100%',
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }) 
    });
   	store.load({params: {start:0,limit:20,creator:userId}});
    store.on('beforeload',function(){
		if(simple.form.isValid()){
			var start_time_from = Ext.get("start_time_from").dom.value;
            var start_time_to = Ext.get("start_time_to").dom.value;
            var end_time_from = Ext.get("end_time_from").dom.value;
            var end_time_to = Ext.get("end_time_to").dom.value;
            if(validate(start_time_from,start_time_to,end_time_from,end_time_to)){
	           if(start_time_from!=''){
	            	start_time_from = start_time_from+" 00:00:00";
	            }
	             if(start_time_to!=''){
	             	start_time_to = start_time_to+" 23:59:59";
	            }
    	        if(end_time_from!=''){
            		end_time_from = end_time_from+" 00:00:00";
            	}
	            if(end_time_to!=''){
	            	end_time_to = end_time_to+" 23:59:59";
		        }
		        Ext.apply(this.baseParams,
			       {creator:userId,
					process_define_id:Ext.get("process_define_id").dom.value,
					process_instance_name:Ext.get("process_instance_name").dom.value,
					process_instance_id:Ext.get("instance_id").dom.value,
					process_instance_desc:Ext.get("process_instance_desc").dom.value,
					start_time_from:start_time_from,
					start_time_to:start_time_to,
					end_time_from:end_time_from,
					end_time_to:end_time_to,
					process_instance_state:Ext.get("process_instance_state").getValue()});
			}else{
				return false;
			}
		}else{
				return false;
		}
    });
   	function showOp(value,cellmeta,record){	
			return "<a href='javascript:doView("+record.data["process_instance_id"]+")'>"+"查看"+"</a>";	
	}
	
	function validate(start_from,start_to,end_from,end_to){
		if(start_from!=''&&start_to!=''){
			var start_time_from = new Date(start_from);
			var start_time_to = new Date(start_to);
			if(start_time_from>start_time_to){
				Ext.MessageBox.alert("流程开始时间范围选取不正确");
				return false;
			}
		}
		if(end_from!=''&&end_to!=''){
			var end_time_from = new Date(end_from);
			var end_time_to = new Date(end_to);
			if(end_time_from>end_time_to){
				Ext.MessageBox.alert("流程结束时间范围选取不正确");
				return false;
			}
		}
		if(start_from!=''&&end_to!=''){
			var end_time_to = new Date(end_to);
			var start_time_from = new Date(start_from);
			if(start_time_from>end_time_to){
				Ext.MessageBox.alert("流程开始时间不能大于流程的结束时间");
				return false;
			}
		}
		return true;
	}	
});

	function doView(process_instance_id){
		 var form = document.forms[0];
		 form.action="${pageContext.request.contextPath}/edu.ccut.saturn.workflow::/publicModule/action/DelineateWorkFlow.action?process_instance_id="+process_instance_id;
		 form.submit();
	}
	function showState(value){
		if(value=='2')
			return "运行";
		if(value=='7')
			return "完成";
	}
</script>
<div id='form-ct' width='100%'></div>
<div id='process-grid' width='100%'></div>
</head>
</html>